package com.winderinfo.yikaotianxia.ui.studycards;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.winderinfo.yikaotianxia.R;
import com.winderinfo.yikaotianxia.core.api.Api;
import com.winderinfo.yikaotianxia.core.base.BaseActivity;
import com.winderinfo.yikaotianxia.core.mvp.Contract;
import com.winderinfo.yikaotianxia.core.mvp.Presenter;
import com.winderinfo.yikaotianxia.core.utils.SPUtils;
import com.winderinfo.yikaotianxia.core.utils.ToastUtil;
import com.winderinfo.yikaotianxia.ui.register.RegisterBean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class StudyCardsActivity extends BaseActivity implements Contract.View {
    @BindView(R.id.stu_studycard)
    EditText stuStudycard;
    @BindView(R.id.stu_pwd)
    EditText stuPwd;
    @BindView(R.id.stu_recycle)
    RecyclerView stuRecycle;
    @BindView(R.id.stu_sum)
    TextView stuSum;
    private Presenter presenter;
    Map<String, Object> map = new HashMap<>();
    private SPUtils spUtils;

    @Override
    protected int initLayoutId() {
        return R.layout.activity_learningcard;
    }

    @Override
    protected void initView() {
        //实例P
        presenter = new Presenter(this);
        spUtils = new SPUtils(this);
        stuRecycle.setLayoutManager(new LinearLayoutManager(this));
    }

    @Override
    protected void initData() {
        //获取用户Id
        String userID = spUtils.getString("UserID", "");
        if (userID != null) {
            //请求优惠券
            /**
             * @id 用户Id
             * */
            map.clear();
            map.put("userId", 1);
            presenter.onPostRequest(Api.STUDYCADS, map, StudyCardsBean.class);
        } else {
            ToastUtil.showSuccess(this, "请先登录");
        }
    }

    @Override
    public void onSuccess(Object data) {
        //学习卡数据
        if (data instanceof StudyCardsBean) {
            List<StudyCardsBean.RowsBean> rows = ((StudyCardsBean) data).getRows();
            stuSum.setText("共有" + rows.size() + "张学习卡");
            StudyCardsAdapter studyCardsAdapter = new StudyCardsAdapter(this, rows);
            stuRecycle.setAdapter(studyCardsAdapter);
        }
        //激活学习卡
        if (data instanceof RegisterBean) {
            if (((RegisterBean) data).getCode() == 0) {
                ToastUtil.showSuccess(this, "激活成功");
            }
            if (((RegisterBean) data).getCode() == 500) {
                ToastUtil.showSuccess(this, ((RegisterBean) data).getMsg());
            }
        }
    }

    @Override
    public void onFailure(Object error) {

    }


    @OnClick({R.id.stu_back, R.id.stu_activation})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.stu_back:
                finish();
                break;
            case R.id.stu_activation:
                //获取输入框内容
                String studycard = stuStudycard.getText().toString();
                String studypwd = stuPwd.getText().toString();
                //判断是否为空
                if (TextUtils.isEmpty(studycard) || TextUtils.isEmpty(studypwd)) {
                    ToastUtil.showSuccess(this, "不能为空");
                    return;
                }
                //请求激活学习卡接口
                /**
                 * @id 用户id
                 * @learnNo 学习卡号
                 * @password 学习卡密码
                 * */
                String userID = spUtils.getString("UserID", "");
                map.clear();
                map.put("userId", 1);
                map.put("learnNo", studycard);
                map.put("password", studypwd);
                presenter.onPostRequest(Api.ACTIVATIONSTUDYCADS, map, RegisterBean.class);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.onUntying();
    }
}
